/*----------------------------------------------------------------------
  - File name      : key.c
  - Author         : Liu Chao
  - Update date    : 2020-07-14                   
  -	Copyright      : Gevico Electronic studio   
  - Module comments: 
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/

#include "key.h"

/*-----------------------------------------------------------------------
|                                 DATA                                  |
-----------------------------------------------------------------------*/

uint8_t Key_Uint_Time = 30;

/*-----------------------------------------------------------------------
|                               FUNCTION                                |
-----------------------------------------------------------------------*/

/*--------------------------------------------------------
| @Description: Key Scan init function                   |
| @param      : None                                     |
| @return     : None                                     |
--------------------------------------------------------*/

void Key_GPIO_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};

	KEY_GPIO_CLK_ENABLE();

	GPIO_InitStruct.Pin = KEY_UP_PIN | KEY_DOWN_PIN ;
	GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	HAL_GPIO_Init(KEY_GPIO_PORT, &GPIO_InitStruct);
	
}

/*--------------------------------------------------------
| @Description: Key Scan one function                    |
| @param      : GPIO_TypeDef  *GPIOx,KEY_MODE Mode       |  
| @return     : None                                     |
--------------------------------------------------------*/

uint16_t Key_Scan_One(GPIO_TypeDef  *GPIOx,KEY_STATE State)
{
	static uint16_t Trg,Cont;
	uint16_t ReadData; 
	uint16_t BackData;

	ReadData = GPIOx -> IDR ^ 0xffff;
	Trg = ReadData & (ReadData ^ Cont);
	Cont = ReadData;

	if(State == KEY_STATE_SING) BackData = Trg;
	else if(State == KEY_STATE_MANY) BackData = Cont;

	return BackData;
}

/*-----------------------------------------------------------------------
|          END OF FLIE        (C) COPYRIGHT Gevico Electronics          | 
-----------------------------------------------------------------------*/

